package com.forum.community.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.forum.community.entity.ForumCommunity;
import com.forum.community.entity.ForumCommunityUser;
import com.forum.community.mapper.ForumCommunityMapper;
import com.forum.community.service.ForumCommunityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.forum.community.service.ForumCommunityUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bncgnc
 * @since 2022-01-04
 */
@Service
public class ForumCommunityServiceImpl extends ServiceImpl<ForumCommunityMapper, ForumCommunity> implements ForumCommunityService {

    @Autowired
    private ForumCommunityUserService forumCommunityUserService;


    @Override
    public int createCommunity(ForumCommunity forumCommunity) {
        int i = baseMapper.insert(forumCommunity);
        //将插入的数据添加到索引表
        Long communityId = forumCommunity.getId();
        Long userId = forumCommunity.getUserId();
        ForumCommunityUser user  = new ForumCommunityUser();
        user.setCommunityId(communityId);
        user.setUserId(userId);
        forumCommunityUserService.getBaseMapper().insert(user);

        return i;
    }

    @Override
    public int deleteCommunity(Long communityId) {
        int i = baseMapper.deleteById(communityId);
        //删除索引表的数据
        QueryWrapper<ForumCommunityUser> query = new QueryWrapper<>();
        query.eq("community_id",communityId);
        forumCommunityUserService.getBaseMapper().delete(query);
        return i;
    }
}
